package edu.ar.modulo2.valentinis.persist;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class PlainPersist<E extends Serializable> {
	
	protected String urlContext;
	protected String fullFileName;
	
	public PlainPersist(String fileName) {		
		this.setDefaultContext();
		this.fullFileName =  this.urlContext + fileName;
	}
	
	public Boolean persist(E entity) {
			
		try {						
			
			File file = new File(this.fullFileName);
			if( file.exists() ) {
				
				FileOutputStream fileStream = new FileOutputStream(this.fullFileName,true);			
				NoHeaderObjectOutputStream objEntity = new NoHeaderObjectOutputStream(fileStream);			
				objEntity.writeUnshared(entity);			
				objEntity.close();
				
			} else {
				
				FileOutputStream fileStream = new FileOutputStream(this.fullFileName);			
				ObjectOutputStream objEntity = new ObjectOutputStream(fileStream);			
				objEntity.writeObject(entity);			
				objEntity.close();
			}
			
		} catch (FileNotFoundException e) {
			
			System.out.print("FileNotFoundException " + this.fullFileName);
			e.printStackTrace();
			
		} catch (IOException e) {

			System.out.print("IOException " + this.fullFileName);
			e.printStackTrace();
		
		} catch (Exception e) {
			
			System.out.print("Exception " + this.fullFileName);
			e.printStackTrace();
		}
					
		return Boolean.FALSE;
	}
	
	public List<E> list() {
		
		List<E> list = new ArrayList<E>();
		ObjectInputStream objectInputStream = null;
		
		try {
			
			FileInputStream inputStream = new FileInputStream(this.fullFileName);
			objectInputStream =  new ObjectInputStream(inputStream);
						
			E entity = null;
			do {
				
				entity = (E) objectInputStream.readObject();				
				list.add(entity);				
				
			} while( entity != null );
			
			objectInputStream.close();
			
		} catch (FileNotFoundException e) {
			
			System.out.print("FileNotFoundException " + this.fullFileName);			
			
		} catch (EOFException e) {
			
			System.out.print("EOFException " + this.fullFileName);			
			
		} catch (IOException e) {
			
			System.out.print("IOException " + this.fullFileName);
			e.printStackTrace();
			
		} catch (ClassNotFoundException e) {
			
			System.out.print("ClassNotFoundException " + this.fullFileName);
			e.printStackTrace();			
		} 
		
		return list;
	}
	
	private void setDefaultContext() {
		
		InputStream inputStream =  Thread.currentThread().getContextClassLoader().getResourceAsStream("persist.properties");		
		Properties properties =  new Properties();
		
		try {
			
			properties.load(inputStream);
			this.urlContext = properties.getProperty("urlContext");
			
		} catch (IOException e) {
			
			System.out.print("NO SE PUEDO CARGAR EL ARCHIVO DE PROPIEDADES: ");
			e.printStackTrace();
		}		
	}
}
